package cqrtplm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.hustcad.plm.pdm.doc.service.TyplmDocumentService;
import com.hustcad.plm.pdm.doc.vo.DocDetails;
import com.hustcad.plm.pdm.ec.module.dto.ECObjData;
import com.hustcad.plm.pdm.ec.module.vo.ChgActivityVO;
import com.hustcad.plm.pdm.ec.module.vo.ChgFormVO;
import com.hustcad.plm.pdm.ec.service.TyplmChgActRecordLinkService;
import com.hustcad.plm.pdm.ec.service.TyplmChgActivityService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChangeIssueView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChangeRequestView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.constant.TableTypeConstrant;
import com.ty.basic.entity.change.ChgActRecordLinkDO;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.service.TaskSubmitVerfiyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TaskSubmitVerfiyServiceImpl implements TaskSubmitVerfiyService {
    private static final int MAX_RETRY = 150; //设置最大重试次数防止死循环
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;
    @Resource
    private TyplmChgActivityService typlmChgActivityService;
    @Resource
    public TyplmDocumentService typlmDocumentService;
    @Resource
    private TyplmChgActRecordLinkService typlmChgActRecordLinkService;

    /**
     * 任务提交校验
     *
     * @param workFlowContainerId
     * @param taskName
     */
    @Override
    public void taskSubmitVerfiy(String workFlowContainerId, String taskName) {
        List<String> errorList = new ArrayList();
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(new BigInteger(workFlowContainerId));
        StringBuffer sb = null;
        if (!Objects.isNull(workFlowContainerView) && workFlowContainerView instanceof FlowChgFormView) {
            if (workFlowContainerView instanceof FlowChangeIssueView) {

            } else if (workFlowContainerView instanceof FlowChangeRequestView) {
                FlowChangeRequestView flowChangeRequestView = (FlowChangeRequestView) workFlowContainerView;
                //获取变更请求对象
            } else if (workFlowContainerView instanceof FlowChgFormView) {
                log.info("当前流程属于变更通告流程");
                FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
                ChgFormVO chgFormVO = flowChgFormView.getChgFormVO();
                String changeNo = chgFormVO.getObjectNumber();
                List<ECObjData> ecObjDataVOS = chgFormVO.getEcObjDataVOS();
                if (CollUtil.isEmpty(ecObjDataVOS))
                    return;
                List<ECObjData> cadDataVOS = ecObjDataVOS.stream().filter(ecObjData -> TableTypeConstrant.TY_CADDOC_OTYPE.contains(ecObjData.getOtype())).collect(Collectors.toList());
                if (CollUtil.isEmpty(cadDataVOS))
                    return;
                if (changeNo.contains("RTTJXM")) {
                    //修模通知单
                } else if (changeNo.contains("RTTJJG")) {
                    //订单更改通知单
                } else if (changeNo.contains("RTTJST")) {
                    //设计更改通知单
                    if ("设计".equals(taskName)) {
                        BigInteger ecaOid = flowChgFormView.getBaseObjectOid();
                        ChgActivityVO chgActivityVO = typlmChgActivityService.queryECAByOid(ecaOid);
                        String description = chgActivityVO.getDescription();
                        if (StringUtils.isNotBlank(description) && description.contains("图文档")) {
                            List<ChgActRecordLinkDO> afterDataLinkDOS=this.typlmChgActRecordLinkService.queryObjOidAndOtypeByECAId(chgActivityVO.getOid());
                            for (ChgActRecordLinkDO cadData : afterDataLinkDOS) {
                                BigInteger oid=cadData.getBoid();
                                String otype = cadData.getBotype();
                                if (TableTypeConstrant.TY_CADDOC_OTYPE.contains(otype)){
                                    DocDetails docDetails = typlmDocumentService.queryDocDetails(oid, otype);
                                    String docNumber = docDetails.getObjectNumber();
                                    String docName = docDetails.getName();
                                    String iterationid = docDetails.getIterationid();
                                    String islastiterated = docDetails.getIslastiterated();
                                    if ("1".equals(iterationid) && "Y".equals(islastiterated)) {
                                        errorList.add(docDetails.getTypeDefName() + ":" + docNumber + "(" + docName + ")");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (errorList.size() > 0) {
            throw RTErrorCodeEnum.CUSTOM_EXCEPTION.getException(new Object[]{StringUtils.join(errorList, ",") + "未修改，请修改后再提交！"});
        }
    }
}
